<template>
  <el-container class="flow-info-container">
    <el-main>
      <div style="position: relative;">
        <el-form ref="formOnlinePage" :model="formOnlinePage" label-width="75px" size="mini" label-position="right" @submit.native.prevent>
          <filter-box :item-width="800">
            <el-row :gutter="40">
              <el-col :span="10">
                <el-form-item label="表单类型" prop="formFilter.pageType">
                  <el-select v-model="formOnlinePage.formFilter.pageType" class="filter-item" placeholder="表单类型" :clearable="true">
                    <el-option v-for="item in SysOnlinePageType.getList()" :key="item.id" :value="item.id" :label="item.name" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="10">
                <el-form-item label="表单名称" prop="formFilter.pageName">
                  <el-input v-model="formOnlinePage.formFilter.pageName" class="filter-item" :clearable="true" placeholder="表单名称" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-button slot="operator" type="default" :plain="true" size="mini" @click="onReset">重置</el-button>
            <el-button slot="operator" type="primary" :plain="true" size="mini" @click="refreshOnlinePage(true)">查询</el-button>
          </filter-box>
        </el-form>
        <el-card shadow="never" class="tableCard">
          <el-row>
            <el-col :span="24">
              <vxe-toolbar :refresh="{query: refresh}">
                <template v-slot:buttons>
                  <el-button type="primary" icon="el-icon-plus" size="small" @click="onCreateFormBasicInfo()">新建</el-button>
                </template>
              </vxe-toolbar>
              <el-table
                ref="class"
                border
                max-height="430"
                :header-cell-style="{background:'#f4f4f4', color:'#717171' }"
                :data="pageListWidget.dataList"
                size="middle"
                header-cell-class-name="table-header-gray"
                @sort-change="pageListWidget.onSortChange"
              >
                <el-table-column label="序号" header-align="center" align="center" type="index" width="55px" :index="pageListWidget.getTableIndex" />
                <el-table-column label="页面名称" prop="pageName" min-width="180px" />
                <el-table-column label="页面代码" prop="pageCode" width="180px" />
                <el-table-column label="页面类型" align="center" width="120px">
                  <template slot-scope="scope">
                    <el-tag size="small" :type="scope.row.pageType === SysOnlinePageType.BIZ ? 'success' : 'primary'">
                      {{ SysOnlinePageType.getValue(scope.row.pageType) }}
                    </el-tag>
                  </template>
                </el-table-column>
                <el-table-column label="页面状态" prop="statusDictMap.name" align="center" width="120px">
                  <template slot-scope="scope">
                    <el-tag size="small" :type="getPageStatusTagType(scope.row.status)">
                      {{ SysOnlinePageStatus.getValue(scope.row.status) }}
                    </el-tag>
                  </template>
                </el-table-column>
                <el-table-column label="发布状态" align="center" width="120px">
                  <template slot-scope="scope">
                    <el-switch v-model="scope.row.published" @change="onUpdatePagePublished(scope.row)" />
                  </template>
                </el-table-column>
                <el-table-column label="创建时间" prop="createTime" align="center" width="180px" />
                <el-table-column label="操作" width="200px" fixed="right">
                  <template slot-scope="scope">
                    <el-button class="table-btn success" type="text" size="small" @click="onEditFormBasicInfo(scope.row)">编辑</el-button>
                    <el-button class="table-btn delete" type="text" size="small" @click="onDeleteOnlinePage(scope.row)">删除</el-button>
                    <el-button class="table-btn success" type="text" size="small" @click="onEditFormDataMode(scope.row)">数据集</el-button>
                    <el-button class="table-btn success" type="text" size="small" @click="onEditFormDesign(scope.row)">表单页面</el-button>
                  </template>
                </el-table-column>
              </el-table>
              <el-col :span="24">
                <el-row type="flex" justify="end" style="margin-top: 10px;">
                  <el-pagination
                    :total="pageListWidget.totalCount"
                    :current-page="pageListWidget.currentPage"
                    :page-size="pageListWidget.pageSize"
                    :page-sizes="[10, 20, 50, 100]"
                    layout="total, prev, pager, next, sizes"
                    @current-change="pageListWidget.onCurrentPageChange"
                    @size-change="pageListWidget.onPageSizeChange"
                  />
                </el-row>
              </el-col>
            </el-col>
          </el-row>
        </el-card>
        <OnlinePageSetting v-if="showPageSetting" :page-id="(currentPage || {}).pageId" @close="onCloseSetting" />
      </div>
    </el-main>
  </el-container>
</template>

<script>
import '@/staticDict/onlineStaticDict.js'
import { mapGetters } from 'vuex'
/* eslint-disable-next-line */
import { TableWidget } from '@/utils/widget.js'
/* eslint-disable-next-line */
import { uploadMixin, statsDateRangeMixin, cachePageMixin } from '@/core/mixins'
/* eslint-disable-next-line */
import { OnlinePageController } from '@/api/onlineController.js'
import OnlinePageSetting from './onlinePageSetting.vue'
import editFormBasicInfo from './editFormBasicInfo.vue'
import editFormDataMode from './editFormDataMode.vue'
import onlineFormDesignList from './onlineFormDesignList.vue'
import FilterBox from '@/components/FilterBox'
import '@/components/Dialog'

export default {
  name: 'FormOnlinePage',
  components: {
    FilterBox,
    OnlinePageSetting
  },
  mixins: [uploadMixin, statsDateRangeMixin, cachePageMixin],
  props: {
  },
  data() {
    return {
      showPageSetting: false,
      currentPage: undefined,
      formOnlinePage: {
        formFilter: {
          pageType: undefined,
          pageName: undefined
        },
        formFilterCopy: {
          pageType: undefined,
          pageName: undefined
        },
        isInit: false
      },
      pageListWidget: new TableWidget(this.loadOnlinePageData, this.loadOnlinePageVerify, true, false)
    }
  },
  computed: {
    ...mapGetters(['getClientHeight'])
  },
  mounted() {
    // 初始化页面数据
    this.formInit()
  },
  methods: {
    onReset() {
      this.$refs.formOnlinePage.resetFields()
      this.refreshOnlinePage(true)
    },
    getPageStatusTagType(status) {
      switch (status) {
        case this.SysOnlinePageStatus.BASIC: return 'warning'
        case this.SysOnlinePageStatus.DATASOURCE: return 'primary'
        case this.SysOnlinePageStatus.DESIGNING: return 'success'
      }
    },
    /**
     * 获取在线页面列表
     */
    loadOnlinePageData(params) {
      if (params == null) params = {}
      params = {
        ...params,
        onlinePageDtoFilter: {
          pageType: this.formOnlinePage.formFilterCopy.pageType,
          pageName: this.formOnlinePage.formFilterCopy.pageName
        }
      }
      return new Promise((resolve, reject) => {
        OnlinePageController.list(params).then(res => {
          resolve({
            dataList: res.data.dataList,
            totalCount: res.data.totalCount
          })
        }).catch(e => {
          reject(e)
        })
      })
    },
    loadOnlinePageVerify() {
      this.formOnlinePage.formFilterCopy.pageType = this.formOnlinePage.formFilter.pageType
      this.formOnlinePage.formFilterCopy.pageName = this.formOnlinePage.formFilter.pageName
      return true
    },
    onCreateFormBasicInfo() {
      this.currentPage = null
      // this.showPageSetting = true
      const params = {}

      this.$dialog.show('新建', editFormBasicInfo, {
        area: '500px'
      }, params).then(res => {
        this.refreshOnlinePage()
      }).catch(e => {})
    },
    onEditFormBasicInfo(row) {
      this.currentPage = row
      // this.showPageSetting = true
      const params = {
        pageId: row.pageId
      }

      this.$dialog.show('编辑基本信息', editFormBasicInfo, {
        area: '500px'
      }, params).then(res => {
        this.pageListWidget.refreshTable()
      }).catch(e => {})
    },
    onEditFormDataMode(row) {
      this.currentPage = row
      // this.showPageSetting = true
      const params = {
        pageId: row.pageId
      }

      this.$dialog.show('编辑数据模型', editFormDataMode, {
        area: '1000px', offset: '100'
      }, params).then(res => {
        this.pageListWidget.refreshTable()
      }).catch(e => {})
    },
    onEditFormDesign(row) {
      this.currentPage = row
      // this.showPageSetting = true
      const params = {
        pageId: row.pageId
      }

      this.$dialog.show('表单页面', onlineFormDesignList, {
        area: ['800px', '480px'], offset: 'auto'
      }, params).then(res => {
        this.pageListWidget.refreshTable()
      }).catch(e => {})
    },
    onDeleteOnlinePage(row) {
      this.$confirm('是否删除此页面？').then(res => {
        const params = {
          pageId: row.pageId
        }

        return OnlinePageController.delete(params)
      }).then(res => {
        this.$message.success('删除成功！')
        this.pageListWidget.refreshTable()
      }).catch(e => {})
    },
    onUpdatePagePublished(row) {
      const params = {
        pageId: row.pageId,
        published: row.published
      }

      OnlinePageController.updatePublished(params).catch(e => {
        // 恢复发布状态为更改之前
        row.published = !row.published
      })
    },
    onCloseSetting() {
      this.currentPage = null
      this.showPageSetting = false
      this.pageListWidget.refreshTable()
    },
    refreshOnlinePage(reloadData) {
      if (reloadData) {
        this.pageListWidget.refreshTable(true, 1)
      } else {
        this.pageListWidget.refreshTable()
      }
      if (!this.formOnlinePage.isInit) {
        // 初始化下拉数据
      }
      this.formOnlinePage.isInit = true
    },
    refresh() {
      this.refreshOnlinePage(true)
    },
    onResume() {
      this.refreshOnlinePage()
    },
    initFormData() {
    },
    formInit() {
      this.refreshOnlinePage()
    }
  }
}
</script>

<style  lang="scss">
  .flow-info-container {
    display: flex;

    flex-direction: column;
    margin: 0 auto;
    .blueLink {
      color: #409eff;
      font-size: 14px;
      margin-left: 20px;
      cursor: pointer;
    }
    .btnList {
      margin-top:20px;
      display: flex;
      justify-content: right;
      float: right;
      .submitBtn {
        margin-right: 30px;
      }
    }
    .fromFlex {
      height: 36px;
      display: flex;
      align-items: center;
    }
    .base-info {
      display: flex;
      margin-bottom: 20px;
      .basics-info {
        width: 100%;
      }
    }
  }
</style>
